/**
 * Webpack config for production electron main process
 */

const path = require("path");
const webpack = require("webpack");
const TerserPlugin = require("terser-webpack-plugin");
const modules = require("./modules");
const paths = require("./paths");

module.exports = function (webpackEnv) {
    const isEnvDevelopment = webpackEnv === "development";
    const isEnvProduction = webpackEnv === "production";

    return {
        devtool: "source-map",

        mode: "production",

        target: "electron-main",

        entry: path.resolve(__dirname, "../src/main/index.ts"),

        output: {
            filename: "main.js",
            path: path.join(__dirname, "..", "build"),
        },

        module: {
            rules: [
                {
                    test: /\.(js|mjs|jsx|ts|tsx)$/,
                    include: [paths.appSrc, isEnvDevelopment && paths.projectDirectory].filter(Boolean),
                    loader: require.resolve("babel-loader"),
                    options: {
                        customize: require.resolve("babel-preset-react-app/webpack-overrides"),

                        plugins: [
                            [
                                require.resolve("babel-plugin-named-asset-import"),
                                {
                                    loaderMap: {
                                        svg: {
                                            ReactComponent: "@svgr/webpack?-svgo,+titleProp,+ref![path]",
                                        },
                                    },
                                },
                            ],
                        ],
                        // This is a feature of `babel-loader` for webpack (not Babel itself).
                        // It enables caching results in ./node_modules/.cache/babel-loader/
                        // directory for faster rebuilds.
                        cacheDirectory: true,
                        // See #6846 for context on why cacheCompression is disabled
                        cacheCompression: false,
                        compact: isEnvProduction,
                    },
                },
                {
                    test: /\.node$/,
                    use: "node-loader",
                },
            ],
        },

        optimization: {
            minimizer: [
                new TerserPlugin({
                    parallel: true,
                    sourceMap: true,
                    cache: true,
                }),
            ],
        },

        plugins: [
            /**
             * Create global constants which can be configured at compile time.
             *
             * Useful for allowing different behaviour between development builds and
             * release builds
             *
             * NODE_ENV should be production so that modules do not perform certain
             * development checks
             */
            new webpack.EnvironmentPlugin({
                NODE_ENV: "production",
                START_MINIMIZED: false,
            }),
        ],

        resolve: {
            extensions: [".js", ".jsx", ".json", ".ts", ".tsx"],
            alias: modules.webpackAliases,
        },

        /**
         * Disables webpack processing of __dirname and __filename.
         * If you run the bundle in node.js it falls back to these values of node.js.
         * https://github.com/webpack/webpack/issues/2010
         */
        node: {
            __dirname: false,
            __filename: false,
        },
    };
};
